﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManageBanking.Areas.Admin.Models
{
    public class ManageAdminModels
    {
        private OnlineBankingDataContext olbk = new OnlineBankingDataContext();

        public List<Username> ListUsername(out int totalUsername)
        {
            totalUsername = olbk.Usernames.Count();
            return olbk.Usernames.OrderByDescending(b=>b.Lock).ToList(); 
        }

        public List<Username> ListLockUsername(out int totalUsername)
        {
            totalUsername = olbk.Usernames.Where(a =>  a.Lock==true).Count();
            return olbk.Usernames.Where(a => a.Lock == true).ToList();
        }


        public List<Username> ListUnlockUsername(out int totalUsername)
        {
            totalUsername = olbk.Usernames.Where(a =>  a.Lock == false).Count();
            return olbk.Usernames.Where(a =>a.Lock == false).ToList();
        }


        public List<RequestAdmin> RequestAdmin(out int totalUsername)
        {

            totalUsername = olbk.RequestAdmins.OrderByDescending(a => a.DateCreate).Count();
            List<RequestAdmin> ra = olbk.RequestAdmins.OrderByDescending(a=>a.DateCreate).ToList();
            return olbk.RequestAdmins.ToList();
        }
        public string NameAdminAll(Username a)
        {
            return olbk.Usernames.Where(b => b.IdUsername == a.Idadmin).Select(b => b.Username1).First();
        }
        public string nameuser(RequestAdmin ra)
        {

            return olbk.Usernames.Where(a => a.IdUsername == ra.IdUsername ).Select(a => a.Username1).First();
        }

        public int iduser(string username)
        {
            return olbk.Usernames.Where(a => a.Username1.Equals(username.Trim())).Select(a => a.IdUsername).First();
        }
        public string nameadmin(RequestAdmin ra)
        {
            string s = "";
            if (olbk.Usernames.Where(a => a.IdUsername == ra.IdAdmin).Count() == 1)
            {
                return s = olbk.Usernames.Where(a => a.IdUsername == ra.IdAdmin).Select(a => a.Username1).First();
            }
            else
                return s;
        }
        
        public void AddAccount(int IdUsername, int IdType)
        {
            Random rd = new Random();
            string nrd = rd.Next(1000, 9999).ToString();

            Account_Username acc = new Account_Username();
            acc.IdAccount = DateTime.Now.ToString("ddMMyyhhmmss") + nrd;
            acc.IdUsername = IdUsername;
            acc.IDType = IdType;
            acc.DateCreate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            acc.Activation = true;
            
            olbk.Account_Usernames.InsertOnSubmit(acc);
            olbk.SubmitChanges();

        }
    }
}
